#!/bin/sh

# $Id: functions.sh 126 2014-07-20 00:03:21Z alan.watson.f@gmail.com $

function TEST_INIT () {
  testfile=$(basename "$1")
  testnumber=0
}

function TEST () {
  printf '%-32.32s ' "$testfile $testnumber"
  mkdir -p /tmp/test
  rm -f /tmp/test/*
  cat - >/tmp/test/text
  sed '/^---/,$d' /tmp/test/text >/tmp/test/stdin
  sed '1,/^---/d' /tmp/test/text >/tmp/test/expected-stdout
  SCHEME_INDENT_FILE=/dev/null \
    chibi-scheme -I ../lib -m scheme-indent.main \
    </tmp/test/stdin >/tmp/test/stdout 2>/tmp/test/stderr
  indentstatus=$?
  diff /tmp/test/expected-stdout /tmp/test/stdout >/tmp/test/diff
  diffstatus=$?
  if test $indentstatus = 0 && test $diffstatus = 0
  then
    printf 'PASSED\n'
  else
    printf 'FAILED\n'
    echo "indentstatus: $indentstatus"
    echo "diffstatus: $diffstatus"
    echo "stdin:"
    cat /tmp/test/stdin
    echo "stderr:"
    cat /tmp/test/stderr
    echo "stdout:"
    cat /tmp/test/stdout
    echo "expected stdout:"
    cat /tmp/test/expected-stdout
    echo "difference between stdout and expected stdout:"
    cat /tmp/test/diff
    exit 1
  fi
  rm -f /tmp/test/*
  testnumber=$(expr $testnumber + 1)
}

function TEST_ERROR () {
  printf '%-32.32s ' "$testfile $testnumber"
  mkdir -p /tmp/test
  rm -f /tmp/test/*
  cat - >/tmp/test/text
  sed '/^---/,$d' /tmp/test/text >/tmp/test/stdin
  sed '1,/^---/d' /tmp/test/text >/tmp/test/expected-stderr
  SCHEME_INDENT_FILE=/dev/null \
    chibi-scheme -I ../lib -m scheme-indent.main \
    </tmp/test/stdin >/tmp/test/stdout 2>/tmp/test/stderr
  indentstatus=$?
  diff /tmp/test/expected-stderr /tmp/test/stderr >/tmp/test/diff
  diffstatus=$?
  if test $indentstatus != 0 && test $diffstatus = 0
  then
    printf 'PASSED\n'
  else
    printf 'FAILED\n'
    echo "indentstatus: $indentstatus"
    echo "diffstatus: $diffstatus"
    echo "stdin:"
    cat /tmp/test/stdin
    echo "stderr:"
    cat /tmp/test/stderr
    echo "stdout:"
    cat /tmp/test/stdout
    echo "expected stderr:"
    cat /tmp/test/expected-stdout
    echo "difference between stderr and expected stderr:"
    cat /tmp/test/diff
    exit 1
  fi
  rm -f /tmp/test/*
  testnumber=$(expr $testnumber + 1)
}
